package com.david.viewtest.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * 横条左右两端为圆头的进度条
 */
class RoundCornerProgressView(
    context: Context,
    attrs: AttributeSet,
) : View(context, attrs) {
    private var bgColor = Color.parseColor("#E6E6E6")
    private var progressColor = Color.parseColor("#FF5841")

    var progress = 10f
        set(value) {
            field = value
            postInvalidate()
        }
    var maxProgress = 100f
        set(value) {
            field = value
            postInvalidate()
        }

    private var paint = Paint()

    private var drawRectF = RectF(0f, 0f, 0f, 0f)

    private var widthFloat = 0f
    private var heightFloat = 0f

    init {
        paint.color = bgColor
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        widthFloat = measuredWidth.toFloat()
        heightFloat = measuredHeight.toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        paint.color = bgColor

        drawRectF.left = 0f
        drawRectF.top = 0f
        drawRectF.right = widthFloat
        drawRectF.bottom = heightFloat

        canvas.drawRoundRect(drawRectF, heightFloat, heightFloat, paint)


        paint.color = progressColor

        drawRectF.right = progress / maxProgress * width

        canvas.drawRoundRect(drawRectF, heightFloat, heightFloat, paint)
    }
}